package com.liming.auth.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liming.auth.mapper.RoleMapper;
import com.liming.auth.service.RolePermissionService;
import com.liming.auth.service.RoleService;
import com.liming.common.pojo.entity.authinfo.Role;
import com.liming.common.pojo.vo.authinfo.RoleVO;
import com.liming.common.util.mybatisplus.MPUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author liming
 * @date 2025/1/20
 */
@Service
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService, MPUtil<Role> {

    private final RolePermissionService rolePermissionService;

    @Override
    public List<RoleVO> usePermissionRoles(Long permissionId) {
        List<Long> useRoleIds = rolePermissionService.userPermissionRoleIds(permissionId);
        if (CollUtil.isEmpty(useRoleIds)) {
            return Collections.emptyList();
        }
        return listByIds(useRoleIds).stream().map(role -> {
            RoleVO vo = new RoleVO();
            BeanUtils.copyProperties(role, vo);
            return vo;
        }).toList();
    }
}
